<script setup lang="ts">
import { fileUploadAPI } from "~/api/fileUpload";
import { buyerComment } from "~/api/order";
import { useImageCache } from "~/composables/useImageCache";

const emits = defineEmits(["close"]);
const props = defineProps({
  orderDetails: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});

const comment = ref("");
const requireClarityScore = ref(5);
const responseScore = ref(5);
const cooperateScore = ref(5);
const fileList = ref([]);
const { getCoverImage } = useImageCache();

const close = (type=null) => {
  emits("close", type);
};

// 上传文件
const uploadFile = async (e: any) => {
  console.log(e);
  const file = e.target.files[0];
  const fileType = file.type;
  if(!fileType.includes('image')){
    ElMessage.error('请上传图片');
    return;
  }
  const formData = new FormData();
  formData.append("file", file);
  const { data } = await fileUploadAPI(formData);
  if (data && data.ossId) {
    // 存储 ossId，后端需要该字段
    fileList.value.push(data.ossId);
  }
  e.target.value = "";
};
const deleteFile = (index: number) => {
  fileList.value.splice(index, 1);
};

// 发布
const submit = async () => {
  try {
    // 根据接口要求，picArray 是 JSONArray 类型，需要是对象数组格式
    // 格式: [{ossId: "xxx"}, {ossId: "yyy"}]
    const picArray = fileList.value.map((ossId) => ({ ossId }));
    
    const data = {
      orderId: props.orderDetails.id || props.orderDetails.orderId,
      buyerComment: comment.value,
      picArray: picArray, // JSONArray 格式，后端会自动解析为 JSONArray
      qualityScore: cooperateScore.value,
      competenceScore: requireClarityScore.value,
      attitudeScore: responseScore.value,
    };
    
    console.log('提交评价数据:', data);
    console.log('picArray 格式:', picArray);
    
    await buyerComment(data);
    ElMessage.success("评价发布成功");
    close("send");
  } catch (e) {
    console.error('评价提交失败:', e);
    ElMessage.error("评价发布失败，请稍后重试");
  }
};
</script>

<template>
  <div class="dialog">
    <div class="dialog-box">
      <div class="header">
        <div class="left">
          <img src="~/assets/dialog-icon.png" alt="" />
          <span>综合评价</span>
        </div>
        <img class="close" src="~/assets/modal-close.png" alt="" @click="close" />
      </div>
      <div class="rate">
        <div>
          <span class="label">专业能力</span>
          <el-rate
            v-model="requireClarityScore"
            size="large"
            allow-half
            :show-score="true"
            void-color="rgba(25, 30, 40, 0.12)"
            :colors="['#1DBF73', '#1DBF73', '#1DBF73']"
          />
          <span class="rate-num">{{ rate1 }}</span>
        </div>
        <div>
          <span class="label">服务态度</span>
          <el-rate
            v-model="responseScore"
            size="large"
            allow-half
            :show-score="true"
            void-color="rgba(25, 30, 40, 0.12)"
            :colors="['#1DBF73', '#1DBF73', '#1DBF73']"
          />
          <span class="rate-num">{{ rate2 }}</span>
        </div>
        <div>
          <span class="label">交付质量</span>
          <el-rate
            v-model="cooperateScore"
            size="large"
            allow-half
            :show-score="true"
            void-color="rgba(25, 30, 40, 0.12)"
            :colors="['#1DBF73', '#1DBF73', '#1DBF73']"
          />
          <span class="rate-num">{{ rate3 }}</span>
        </div>
        <div>
          <textarea v-model="comment" placeholder="留下你的评价吧…"></textarea>
        </div>
        <p class="upload">
          <span class="upload-img" v-for="(item, index) in fileList" :key="index"
            ><img class="img" :src="getCoverImage(item)" alt="" />
            <img
              @click="deleteFile(index)"
              class="del"
              src="~/assets/img-del-icon.png"
              alt=""
          /></span>
          <span class="upload-add" v-if="fileList.length <= 5"
            ><img src="~/assets/file-add.png" alt="" />
            <input type="file" accept="image/png, image/jpeg" @change="uploadFile"
          /></span>
        </p>
      </div>
      <div class="footer">
        <span class="btn" @click="submit">发布</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.dialog {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(25, 30, 40, 0.4);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-box {
  width: 400px;
  background: #ffffff;
  border-radius: 12px 12px 12px 12px;
  padding: 24px;

  .header {
    display: flex;
    align-items: center;
    padding-bottom: 20px;

    .left {
      flex: 1;
      display: flex;
      align-items: center;

      img {
        width: 20px;
        height: 20px;
        margin-right: 4px;
      }

      span {
        font-size: 16px;
        color: #191e28;
        font-weight: bold;
      }
    }

    .close {
      width: 18px;
      height: 18px;
      cursor: pointer;
    }
  }

  .rate {
    & > div {
      display: flex;
      align-items: center;
      margin-bottom: 6px;
    }

    .label {
      padding-right: 16px;
      font-weight: bold;
      color: #191e28;
    }

    .rate-num {
      color: #191e28;
      line-height: 24px;
      font-weight: bold;
      margin-left: 4px;
    }

    textarea {
      width: 100%;
      height: 96px;
      background: rgba(25, 30, 40, 0.02);
      border-radius: 12px 12px 12px 12px;
      border: 1px solid rgba(25, 30, 40, 0.08);
      resize: none;
      padding: 12px 16px;
      box-sizing: border-box;
    }
  }
  .upload {
    display: flex;
    margin-top: 10px;
    .upload-img {
      width: 50px;
      height: 50px;
      margin-right: 12px;
      border-radius: 12px;
      overflow: hidden;
      position: relative;
      .img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      .del {
        position: absolute;
        top: 2px;
        right: 2px;
        width: 16px;
        height: 16px;
        cursor: pointer;
        transition: all 0.3s;
        &:hover {
          transform: scale(1.4);
        }
      }
    }
    .upload-add {
      flex: 0 0 50px;
      height: 50px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
      }
      input {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        opacity: 0;
      }
    }
  }
  .footer {
    display: flex;
    padding: 32px 0 0;
    justify-content: flex-end;
    .btn {
      width: 92px;
      height: 36px;
      background: #1dbf73;
      border-radius: 8px 8px 8px 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      cursor: pointer;
    }
  }
}
</style>
<style>
.rate .el-rate .el-rate__icon {
  font-size: 24px;
  margin-right: 0;
}
</style>
